package com.hyz.service.permission.impl;

import client.AssetClient;
import com.hyz.model.dto.ApplicantAsRequest;
import com.hyz.model.vo.*;
import com.hyz.service.permission.ApplicantService;
import com.hyz.util.AssetSecondmentUtils;
import com.hyz.util.Conversion;
import com.hyz.util.GetUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.ResponseResult;
import pojo.asset.asset.Asset;
import pojo.asset.asset.AssetClasses;
import pojo.asset.assetMaintenance.AssetMaintenance;
import pojo.asset.assetRetirement.AssetRetirement;
import pojo.asset.assetSecondment.AssetSecondment;
import pojo.asset.assetSecondment.AssetSecondmentStatus;

import java.util.*;

/**
 * @author HGD
 * @date 2022/10/9 0:06
 */
@Service
public class ApplicantServiceImpl implements ApplicantService {
    private final int code = 200;

    @Autowired
    private AssetClient assetClient;
    @Autowired
    private Conversion conversion;
    @Autowired
    private AssetSecondmentUtils asUtils;
    @Autowired
    private GetUser getUser;


    @Override
    public ResponseResult<GetAllowBorrowAssetsResponse> getAllowBorrowAssets() {
        ResponseResult<List<Asset>> result = assetClient.getAllowBorrowAssets();
        if (result.getStatus() != code) {
            return new ResponseResult<>(result.getStatus(), result.getMsg());
        }

        Map<AssetClasses, List<String>> hashMap = new HashMap<>();
        List<Asset> data = result.getData();
        for (Asset datum : data) {
            if (hashMap.containsKey(datum.getClasses())) {
                List<String> list = hashMap.get(datum.getClasses());
                list.add(datum.getAssetName());
                hashMap.put(datum.getClasses(), list);
                continue;
            }
            List<String> list = new ArrayList<>();
            list.add(datum.getAssetName());
            hashMap.put(datum.getClasses(), list);
        }
        GetAllowBorrowAssetsResponse response = new GetAllowBorrowAssetsResponse();
        response.setAssetNameAndClasses(hashMap);
        List<AssetSecondmentStatus> list = new ArrayList<>();
        list.add(AssetSecondmentStatus.BORROW);
        list.add(AssetSecondmentStatus.TRANSFERS);
        response.setAssetSecondmentStatuses(list);
        return new ResponseResult<>(result.getStatus(), result.getMsg(), response);
    }

    @Override
    public ResponseResult<List<AssetSecondmentResponse>> getAssetSecondments() {
        Long applicantId = getUser.getUser().getId();
        
        ResponseResult<List<AssetSecondment>> result = assetClient.getAssetSecondments(applicantId);
        return asUtils.getAssetSecondmentResponse(result);
    }

    @Override
    public ResponseResult<List<AssetSecondmentResponse>> insertAssetSecondment(ApplicantAsRequest request) {
        Long applicantId = getUser.getUser().getId();

        String assetName = request.getAssetName();
        AssetClasses assetClasses = request.getAssetClasses();
        String secondmentReason = request.getSecondmentReason();
        AssetSecondmentStatus status = request.getStatus();

        ResponseResult<List<AssetSecondment>> result = assetClient.insertAssetSecondment(assetName, assetClasses, applicantId, secondmentReason, status);
        return asUtils.getAssetSecondmentResponse(result);
    }

    @Override
    public ResponseResult<List<AssetMaintenanceResponse>> apGetAssetMaintenance() {
        Long applicantId = getUser.getUser().getId();
        ResponseResult<List<AssetMaintenance>> result = assetClient.apGetAssetMaintenance(applicantId);
        if (!Objects.equals(result.getStatus(), code)) {
            return new ResponseResult<>(result.getStatus(), result.getMsg());
        }
        List<AssetMaintenanceResponse> responses = conversion.getAssetMaintenanceResponse(result.getData());
        return new ResponseResult<>(result.getStatus(), result.getMsg(), responses);
    }

    @Override
    public ResponseResult<List<AssetMaintenanceResponse>> insertAssetMaintenance(Long assetId) {
        Long applicantId = getUser.getUser().getId();
        ResponseResult<Boolean> result = assetClient.apInsertAssetMaintenance(assetId, applicantId);
        if (!result.getData()) {
            return new ResponseResult<>(result.getStatus(), result.getMsg());
        }
        return apGetAssetMaintenance();
    }

    @Override
    public ResponseResult<List<AssetRetirementResponse>> apGetAssetRetirement() {
        Long applicantId = getUser.getUser().getId();
        ResponseResult<List<AssetRetirement>> result = assetClient.apGetAssetRetirement(applicantId);
        if (result.getStatus() != code) {
            return new ResponseResult<>(result.getStatus(), result.getMsg());
        }
        List<AssetRetirementResponse> responses = conversion.getAssetRetirementResponse(result.getData());
        return new ResponseResult<>(result.getStatus(), result.getMsg(), responses);
    }

    @Override
    public ResponseResult<List<AssetRetirementResponse>> insertAssetRetirement(Long assetId) {
        Long applicantId = getUser.getUser().getId();
        ResponseResult<Boolean> result = assetClient.apInsertAssetRetirement(assetId, applicantId);
        if (!result.getData()) {
            return new ResponseResult<>(result.getStatus(), result.getMsg());
        }
        return apGetAssetRetirement();
    }

    @Override
    public ResponseResult<Map<String, List<GetAllowBorrowAssetsByClassesResponse>>> getAllowBorrowAssetsByClasses(AssetClasses classes) {
        ResponseResult<List<Asset>> result = assetClient.getAllowBorrowAssetsByClasses(classes);
        if (result.getStatus() != code) {
            return new ResponseResult<>(result.getStatus(), result.getMsg());
        }

        Map<String, List<GetAllowBorrowAssetsByClassesResponse>> map = new HashMap<>(1);
        List<GetAllowBorrowAssetsByClassesResponse> list = new ArrayList<>();
        for (Asset datum : result.getData()) {
            list.add(GetAllowBorrowAssetsByClassesResponse.builder()
                    .label(datum.getAssetName())
                    .value(datum.getAssetName()).build());
        }
        map.put("options", list);
        return new ResponseResult<>(result.getStatus(), result.getMsg(), map);
    }
}
